using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System;

namespace HareGame
{
    public class TaskItemUI : PoolObj
    {
        public Toggle IsComplete;
        public Text txtDes;
        public Text txtReward;
        public Text txtProgress;
        public MyButton btnClaim;
        public MyButton btnGo;
        public GameObject objMoney;
        public GameObject objDiamond;

        private TaskData _data;
        private Action _onComplete;
        private Action<TaskData> _onGo;

        public void Init(TaskData data, Action onComplete, Action<TaskData> onGo)
        {
            _data = data;
            _onComplete = onComplete;
            _onGo = onGo;
            RefreshUI();
        }

        protected override void DoEvent(EventID eventID, params object[] param)
        {
            base.DoEvent(eventID, param);
            switch (eventID)
            {
                case EventID.RefreshTaskUI:
                case EventID.RefreshBuildingUI:
                case EventID.RefreshBuildingItemUI:
                    RefreshUI();
                    break;
            }
        }

        void RefreshUI()
        {
            IsComplete.isOn = _data.IsReady2Complete;
            txtDes.text = GameEntry.Localization.GetValue(_data.Des);
            txtReward.text = "+" + _data.Reward.int2String();
            txtProgress.text = _data.CurCount + "/" + _data.NeedCount;
            objMoney.SetActive(_data.RewardType == 1);
            objDiamond.SetActive(_data.RewardType == 2);
            btnGo.gameObject.SetActive(!_data.IsReady2Complete);
        }

        public void OnClaimClick()
        {
            if (_data.RewardType == 1)
            {
                GameEntry.Event.Send(EventID.AddReward, 2, _data.Reward * 1d);
            }
            else
            {
                GameEntry.Event.Send(EventID.AddReward, 1, _data.Reward * 1d);
            }
            GameEntry.Task.CompleteTask(_data);
            _onComplete();
        }

        public void OnMoveClick()
        {
            _onGo(_data);
        }
    }
}
